<?php
/**
 * Geo filter
 * 
 * @author MF
 * @version 2.0 
 */

require_once 'Zend/Controller/Action.php';

class Cms_GeoController extends Zend_Controller_Action {
	
	function init() {
		CmsAccess::restrictModule('geofilter');
	}
	
	public function indexAction() {
		
		$this->view->geoList = MF_Main::getInstance()->Geo;
	}
	
	
	public function addAction() {
		
		if ($this->_request->isPost()) {
			$post_data = $this->_request->getPost();
			if ($post_data['name']=='') {
				$this->view->error_msg = 'Название не может быть пустым';
			} else {
				MF_Main::getInstance()->Geo->add()->value = $post_data['name'];
				$this->render('exit');
				$this->_redirect('/cms/geo');
				return true;
			}
			
		}
		$this->render('form');
	}
	
	
	public function editAction() {
		
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "id"', 404);
		}
		
		$geoItem = MF_Main::getInstance()->Geo->getById($id);
		
		if ($this->_request->isPost()) {
			$post_data = $this->_request->getPost();
			if ($post_data['name']=='') {
				$this->view->error_msg = 'Название не может быть пустым';
			} else {
				$geoItem->value = $post_data['name'];
				$this->render('exit');
				$this->_redirect('/cms/geo');
				return true;
			}
			
		}
		
		$this->view->value = $geoItem->value;
		$this->render('form');
	}
	
	public function actAction() {
		
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Required param "id"', 404);
		}
		
		if (NULL === $type = $this->getRequest()->getParam('type')) {
			throw new Zend_Controller_Action_Exception('Required param "type"', 404);
		}
		
		$geoList = MF_Main::getInstance()->Geo;
		$geoItem = $geoList->getById($id);
		switch ($type) {
			case 'up':
				$geoItem->sequence = $geoItem->sequence - 1;
				break;
				
			case 'down':
				$geoItem->sequence = $geoItem->sequence + 1;
				break;
			
			case 'del':
				$geoItem->delete();
				break;
			
			default:
				throw new Zend_Controller_Action_Exception('Invalid value for param "Type"', 404);
				break;
		}
		
		$this->view->geoList = $geoList;
		
		
	}
	
	
}